package com.pk.platform.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.constant.Constant;

/**
 * 在线计数器
 * @author
 */
public class OnlineCounter {

	private static Map<String, String> USER_MAP = new HashMap<String, String>();
	
	public static void create()
	{
		HttpSession session = getHttpSession();
		User user = (User)session.getAttribute(Constant.USER);
		if(user != null && USER_MAP.get(user.getId()) == null)
		{
			USER_MAP.put(user.getId(), "");
		}
	}

	public static void remove(HttpSession session)
	{
		User user = (User)session.getAttribute(Constant.USER);
		if(user != null && USER_MAP.get(user.getId()) != null)
		{
			USER_MAP.remove(user.getId());
		}
	}
	
	public static int getOnlineCount()
	{
		return USER_MAP.keySet().size();
	}
	
	private static HttpSession getHttpSession()
	{
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		return session;
	}
}
